package system

/**
 * 权限角色 关联的中间表    --新增
 */
class RoleAuthority implements Serializable {
    /*创建时间*/
    Date dateCreated
    /** 最后更新的时间 */
    Date lastUpdated
    static belongsTo = [role: Role, authority: Authority]
    static constraints = {
        dateCreated nullable: true
        lastUpdated nullable: true
    }
    static mapping = {
        id composite: ['role', 'authority']
        version false
    }

    String toString() {
        "{$role.id} {$authority.id} "
    }

    boolean equals(other) {
        if (!(other instanceof RoleAuthority)) {
            return false
        }

        other.role?.id == role?.id &&
                other.authority?.id == authority?.id
    }


    static RoleAuthority get(long roleId, long authorityId) {
        //在control中使用RoleAuthority.get(roleId,authorityId) 就能得到实体
        find 'from RoleAuthority where role.id=:roleId and authority.id=:authorityId',
                [roleId: roleId, authorityId: authorityId]
    }

    static RoleAuthority create(Role role, Authority authority, boolean flush = false) {
        //在control中使用RoleAuthority.create(role,authority) 就能创建实体
        def result = null
        if (role && authority) {
            if (!RoleAuthority.findByAuthorityAndRole(authority, role)) {
//              new RoleAuthority(role: role, authority: authority).save(flush: flush, insert: true)
//                println("--->"+RoleAuthority.get(role.id,authority.id)?.toString())
                try {
                    result = new RoleAuthority(role: role, authority: authority).save(flush: flush)
                    if (!result) {
                        result.errors.each {
                            println(it)
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace()
                }

            } else {
                println("角色" + role + "添加角色 " + authority + " 的关联已经存在,没有继续添加关联")
                result = null
            }

        }
        result

    }

    static boolean remove(Role role, Authority authority, boolean flush = false) {
        // 在control中使用RoleAuthority.remove(role,authority) 就能创建实体
//		def instance = RoleAuthority.findByRoleAndAuthority(role, authority)
//		if (!instance) {
//			return false
//		}
        println("从角色" + role.id + "  " + role?.name + " 移除权限 " + authority.id + " " + authority?.authority)
        //instance.delete(flush: flush)  //如果Many端存在于One端的集合中，直接删除Many，就会抛出"deleted object would be re-saved by cascade"的异常
        executeUpdate 'DELETE FROM RoleAuthority WHERE role.id=:role and authority.id=:authority', [role: role?.id, authority: authority?.id]
        true
    }

    static void removeAll(Role role) {
        executeUpdate 'DELETE FROM RoleAuthority WHERE role=:role', [role: role]
    }

    static void removeAll(Authority authority) {
        executeUpdate 'DELETE FROM RoleAuthority WHERE authority=:authority', [authority: authority]
    }
    //2012-2-15 fang add  取得某一角色下所有的权限
    static Set<Authority> getAuthoritys(Role role) {
        if (role)
            return RoleAuthority.findAllByRole(role).authority
        else {
            return null
        }
    }
}
